/**
 * 
 */
package com.zhang.junit;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.junit.Test;

import com.zhang.mappers.oradeMapper;
import com.zhang.mappers.usermapperDao;
import com.zhang.pojo.Orders;
import com.zhang.pojo.User;
import com.zhang.pojo.queryVo;

/**
 * @张博皓
 *2017年11月17日
 */
public class mapperTest {
	private usermapperDao mapper;

	@Test
	public void testMapper() throws Exception {
	String resource ="sqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
		//sqlsession生成实现类---给接口
	usermapperDao mapper = sqlsession.getMapper(usermapperDao.class);
	
	User load = mapper.load(10);
	System.out.println(load);
	

	}
	
	
	@Test
	public void testMapperQuery() throws Exception {
	String resource ="SqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
		//sqlsession生成实现类---给接口
	usermapperDao mapper = sqlsession.getMapper(usermapperDao.class);
	
	queryVo vo=new queryVo();
	User user=new User();
	user.setUsername("五");
    vo.setUser(user);
	List<User> loads = mapper.findorlikeidQueryVo(vo);
	for (User user2 : loads) {
		System.out.println(user2);
	}
	}
	
	@Test
	public void testMapperCountUser() throws Exception {
	String resource ="SqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
		//sqlsession生成实现类---给接口
	usermapperDao mapper = sqlsession.getMapper(usermapperDao.class);
	
	 Integer countUser = mapper.countUser();
	System.out.println(countUser);
	}
	
	
	//查询订单表的所有数据
	@Test
	public void testMapperOrades() throws Exception {
	String resource ="SqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
		//sqlsession生成实现类---给接口
	oradeMapper mapper = sqlsession.getMapper(oradeMapper.class);
	
	List<Orders> i=mapper.selectOrdersList();
	for (Orders orders : i) {
		System.out.println(orders);
	}
	}
	
	//根据姓名和性别查询用户
	@Test
	public void testMapperUser() throws Exception {
	String resource ="SqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
	//sqlsession生成实现类---给接口
	 usermapperDao mapper2 = sqlsession.getMapper(usermapperDao.class);
	 User user=new User();
	 user.setSex("1");
//	 user.setUsername("陈小明");
	 List<User> selectuserBySexAndusername = mapper2.selectUserBySexAndUsername(user);
	 for (User user2 : selectuserBySexAndusername) {
		System.out.println(user2);
	}
	}
	//根据多个id查询用户
	@Test
	public void testMapperUserid() throws Exception {
	String resource ="SqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
	//sqlsession生成实现类---给接口
	 usermapperDao mapper2 = sqlsession.getMapper(usermapperDao.class);
	
//	 List<Integer> ids=new ArrayList<Integer>();
//	 ids.add(16);
//	 ids.add(22);
//	 ids.add(24);
//	 queryVo vo=new queryVo();
//	 vo.setIdsList(ids);
	 
	 Integer[] ids=new Integer[3];
	 ids[0]=16;
	 ids[1]=22;
	 ids[2]=24;
	 
	 List<User> user = (mapper2).selectUserByidtwo(ids);
	 for (User user2 : user) {
		System.out.println(user2);
	}
	}
	
	
	
	
}